let q = {
    pagenum: 1,
    pagesize: 2,
    cate_id: '',
    state: ''
}

let total = 0
let article_arr = []

// 获取分类列表
function getCateList() {
    axios({
        method: 'get',
        url: '/my/article/cates',
    }).then(res => {
        let options_arr = res.data.data.map(item => {
            return `
            <option value="${item.Id}">${item.name}</option>
            `
        })
        $('#select_cate').append(options_arr.join(''))
        layui.form.render()
    })
}
getCateList()

// 获取文章列表
function getArticleList() {
    axios({
        method: 'get',
        url: '/my/article/list',
        params: q
    }).then(res => {
        if (res.data.data.length === 0 && q.pagenum > 1) {
            q.pagenum--
                getArticleList()

            total = res.data.total
            article_arr = res.data.data
        }
        let trs_arr = res.data.data.map(item => {
            return `
            <tr>
                <td>${item.title}</td>
                <td>${item.cate_name}</td>
                <td>${item.pub_date}</td>
                <td>${item.state}</td>
                <td><button type="button" class="layui-btn layui-btn-xs edit" data-id="${item.Id}">编辑</button>
                <button type="button" class="layui-btn layui-btn-danger layui-btn-xs " id="delete" data-id="${item.Id}">删除</button>
                </td>   
            </tr>
            `
        })
        $('tbody').html(trs_arr.join(''))

        // 渲染分页
        layui.use('laypage', function() {
            var laypage = layui.laypage;

            //执行一个laypage实例
            laypage.render({
                elem: 'pages',
                count: res.data.total, //数据总数，从服务端得到
                layout: ['count', 'prev', 'page', 'next', 'limits', 'limit', 'curr'],
                curr: q.pagenum,
                limit: q.pagesize,
                limits: [2, 4, 6, 8],
                jump: function(obj, first) {
                    //首次不执行
                    if (first) {
                        return
                    }
                    q.pagenum = obj.curr
                    q.pagesize = obj.limit
                    getArticleList()
                }
            });
        });
    })
}
getArticleList()